//
//  QLMQRCodeVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/11/8.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMQRCodeVC.h"
#import "CreatQRCodeAndBarCode.h"
#import "QLBxOrderCostVC.h"

@interface QLMQRCodeVC (){
    
    NSTimer *timer;
}

@end

@implementation QLMQRCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"POS机支付";
    
    timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(requestMoneyInfoData) userInfo:nil repeats:YES];
    
    NSDate *second = [NSDate date];
    long secondTimeZone = [second timeIntervalSince1970]*1000;
    NSDictionary *pardic=[NSDictionary dictionaryWithObjectsAndKeys:@(secondTimeZone),@"date",_orderId,@"orderNo", nil];
    
    // Do any additional setup after loading the view.
    UIImageView *codeImageView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 40, kScreen_width-40, kScreen_width-40)];
    codeImageView.image=[CreatQRCodeAndBarCode qrImageWithString:[self dictionaryToJson:pardic]
                                                            size:CGSizeMake(kScreen_width,kScreen_width)
                                                           color:[UIColor darkGrayColor]
                                                 backGroundColor:[UIColor whiteColor]];
    [self.view addSubview:codeImageView];
}

- (NSString*)dictionaryToJson:(NSDictionary *)dic{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [timer setFireDate:[NSDate date]];
}

- (void)pushToCustomVC{
    for (UIViewController *VC in self.navigationController.viewControllers) {
        if ([VC isKindOfClass:[QLBxOrderCostVC class]]) {
            QLBxOrderCostVC *costVC=(QLBxOrderCostVC *)VC;
            costVC.isBXPayMoneyJumpBool=YES;
            [self.navigationController popToViewController:VC animated:YES];
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)leftClick{
    [self clearTimer];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc {
    [self clearTimer];
}

- (void)clearTimer{
    [timer invalidate];
    timer = nil;
}

- (void)requestMoneyInfoData{
    NSMutableDictionary *paramerDic=[NSMutableDictionary dictionary];
    [paramerDic setObject:_orderId forKey:@"orderNo"];
    [ClientTool postUrlPath:@"/rest/tl/payResult" withParamers:paramerDic andBody:nil
                    success:^(id responseObject){
                        [self.view hideToastActivity];
                        NSInteger code = [[responseObject objectForKey:@"flag"] integerValue];
                        if (code == 1) {
                            if ([responseObject[@"status"] isEqualToString:@"2"]) {
                                ETToast(responseObject[@"msg"]);
                                [self clearTimer];
                                [self pushToCustomVC];
                            }
                        }else{
                            ETToast(responseObject[@"msg"]);
                        }
                    }failure:^(NSError *error){
                        [self.view hideToastActivity];
                        ETToast(@"服务器连接失败");
                    }
     ];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
